package com.xukai.springboot.datalayer.statis;

import com.xukai.springboot.entity.statistics.t_statis_chart_setting;
import com.xukai.springboot.entity.sys.t_sys_chart_type;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 图表设置mapper
 */
@Mapper
public interface ChartSettingMapper {

    /**
     * 获取图表设置列表
     */
    @SelectProvider(type = ChartSettingMapperProvider.class, method = "GetChartSettingList")
    public List<t_statis_chart_setting> GetChartSettingList(@Param("chartType") int chartType, @Param("chartName") String chartName, @Param("start") int start
            , @Param("pageSize") int pageSize, @Param("state") int state);

    /**
     * 获取图表设置总数
     */
    @SelectProvider(type = ChartSettingMapperProvider.class, method = "GetChartSettingCount")
    @ResultType(Integer.class)
    public int GetChartSettingCount(@Param("chartType") int chartType, @Param("chartName") String chartName, @Param("state") int state);

    /**
     * 新建图表设置信息
     */
    @Insert("INSERT INTO t_statis_chart_setting (id,chartType,chartName,state,`data`,createTime,`describe`)\n" +
            "VALUES(#{id},#{chartType},#{chartName},#{state},#{data},#{createTime},#{describe})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddChartSetting(t_statis_chart_setting data);

    /**
     * 获取图表设置数据
     */
    @Select(" select a.chartName,a.chartType,a.createTime,a.id,a.state,b.`name`  as chartTypeName ,a.`describe`,a.data from t_statis_chart_setting a " +
            "join  t_sys_chart_type b on a.chartType=b.id " +
            "where a.id=#{id}")
    public t_statis_chart_setting GetChartSettingDetail(@Param("id") int id);

    /**
     * 修改图表基本信息
     */
    @Update("UPDATE  t_statis_chart_setting set chartType=#{chartType},chartName=#{chartName}, `describe`=#{describe}  ,state=#{state} where id=#{id} ")
    public void UpdateChartSetting(t_statis_chart_setting data);

    /**
     * 修改图表json设置
     */
    @Update("UPDATE  t_statis_chart_setting set data=#{data},state=#{state} where id=#{id} ")
    public void UpdateChartSettingJsonData(@Param("id") int id, @Param("data") String data, @Param("state") int state);

    /**
     * 删除图表设置
     */
    @Delete("DELETE FROM t_statis_chart_setting  where id=#{id}")
    public void DelChartSetting(@Param("id") int id);

    /**
     * 图表设置mapper 辅助类
     */
    class ChartSettingMapperProvider {

        /**
         * 获取图表设置列表
         *
         * @param chartType
         * @param chartName
         * @return
         */
        public String GetChartSettingList(int chartType, String chartName, int start, int pageSize, int state) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select a.chartName,a.chartType,a.createTime,a.`describe`,a.id,a.state,b.`name` as chartTypeName from t_statis_chart_setting a");
            sb.append(" join  t_sys_chart_type b on a.chartType=b.id");
            sb.append(" where 1=1");
            if (chartType > 0) {
                sb.append(" and chartType=#{chartType}");
            }
            if (!chartName.isEmpty()) {
                //sb.append(" and chartName like'%#{chartName}%'");
                sb.append(" and dataSourceName like concat(concat('%',#{chartName}),'%') ");
            }
            if (state > -1) {
                sb.append(" and a.state=#{state}");
            }
            sb.append(" order by chartName");
            if (pageSize > -1) {
                sb.append(" limit #{start},#{pageSize}");
            }


            return sb.toString();
        }

        /**
         * 获取图表设置列表总数
         *
         * @param chartType
         * @param chartName
         * @return
         */
        public String GetChartSettingCount(int chartType, String chartName, int state) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count(a.id) from t_statis_chart_setting a");
            sb.append(" join  t_sys_chart_type b on a.chartType=b.id");
            sb.append(" where 1=1");
            if (chartType > 0) {
                sb.append(" and a.state=#{state}");
            }
            if (state > -1) {
                sb.append(" and chartType=#{chartType}");
            }
            if (!chartName.isEmpty()) {
                //sb.append(" and chartName like'%#{chartName}%'");
                sb.append(" and dataSourceName like concat(concat('%',#{chartName}),'%') ");
            }

            return sb.toString();
        }


    }
}
